package com.hejia.alauda.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 打款明细
 * @author hyr
 *
 */
public class RepaymentLedger {
	
	private Long repaymentOrderId; // 打款订单ID
	private Long userId; // 用户ID
	private String description; // 打款描述
	private Long productId; // 产品ID
	private String productName; // 产品名称
	private String userName; // 收款人姓名
	private BigDecimal repayAmount; // 打款金额
	private BigDecimal financingAmount; // 本金
	private BigDecimal incomeAmount; // 利息
	private BigDecimal register50RedEnvelope; // 50注册红包
	private BigDecimal financialRedEnvelope; // 理财金
	private List<RepaymentBonus> repaymentBonuses; // 红包列表
	private BigDecimal sumRedEnvelope; // 计算的红包合计

	public Long getRepaymentOrderId() {
		return repaymentOrderId;
	}

	public void setRepaymentOrderId(Long repaymentOrderId) {
		this.repaymentOrderId = repaymentOrderId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getProductId() {
		return productId;
	}

	public void setProductId(Long productId) {
		this.productId = productId;
	}
	
	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public BigDecimal getRepayAmount() {
		return repayAmount;
	}

	public void setRepayAmount(BigDecimal repayAmount) {
		this.repayAmount = repayAmount;
	}

	public BigDecimal getFinancingAmount() {
		return financingAmount;
	}

	public void setFinancingAmount(BigDecimal financingAmount) {
		this.financingAmount = financingAmount;
	}

	public BigDecimal getIncomeAmount() {
		return incomeAmount;
	}

	public void setIncomeAmount(BigDecimal incomeAmount) {
		this.incomeAmount = incomeAmount;
	}

	public BigDecimal getRedEnvelope() {
		return this.repayAmount.subtract(this.financingAmount).subtract(this.incomeAmount);
	}

	public BigDecimal getRegister50RedEnvelope() {
		// 去除用户一天有多笔兑付，老红包金额重复问题
		// 红包金额与计算的红包合计相等，认为老红包是有效的
		if (this.getRedEnvelope().compareTo(getSumRedEnvelope()) == 0) {
			return register50RedEnvelope;
		}
		return BigDecimal.ZERO;
	}

	/**
	 * 计算红包合计金额
	 * @return
	 */
	private BigDecimal getSumRedEnvelope() {
		if (this.sumRedEnvelope == null) {
			sumRedEnvelope = BigDecimal.ZERO;
			sumRedEnvelope = this.register50RedEnvelope;
			sumRedEnvelope = sumRedEnvelope.add(this.financialRedEnvelope);
			for (RepaymentBonus repaymentBonus : this.getRepaymentBonuses()) {
				sumRedEnvelope = sumRedEnvelope.add(repaymentBonus.getAmount());
			}
		}
		return sumRedEnvelope;
	}

	public void setRegister50RedEnvelope(BigDecimal register50RedEnvelope) {
		this.register50RedEnvelope = register50RedEnvelope;
	}

	public BigDecimal getFinancialRedEnvelope() {
		// 去除用户一天有多笔兑付，老红包金额重复问题
		// 红包金额与计算的红包合计相等，认为老红包是有效的
		if (this.getRedEnvelope().compareTo(getSumRedEnvelope()) == 0) {
			return financialRedEnvelope;
		}
		return BigDecimal.ZERO;
	}

	public void setFinancialRedEnvelope(BigDecimal financialRedEnvelope) {
		this.financialRedEnvelope = financialRedEnvelope;
	}
	
	public List<RepaymentBonus> getRepaymentBonuses() {
		if (this.repaymentBonuses == null) {
			this.repaymentBonuses = new ArrayList<>();
		}
		return repaymentBonuses;
	}

	public void setRepaymentBonuses(List<RepaymentBonus> repaymentBonuses) {
		this.repaymentBonuses = repaymentBonuses;
	}
}
